package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.RedisConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.exception.BaseException;
import com.sky.mapper.admin.CategoryManagementMapper;
import com.sky.pojo.Category;
import com.sky.result.PageResult;
import com.sky.service.admin.CategoryManagementService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description FoodManagementImpl
 * @Author luohai
 * @Date 2024-09-30
 */
@Service
public class CategoryManagementImpl implements CategoryManagementService {
    @Autowired
    private CategoryManagementMapper categoryManagementMapper;
    /**
     * 新增分类
     * @param categoryDTO
     * @return
     */
    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category1 = categoryManagementMapper.searchCategoryByName(categoryDTO.getName());
        if (category1 != null){
            throw new BaseException("分类存在");
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setStatus(StatusConstant.DISABLE);
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryManagementMapper.addCategory(category);
    }

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult classificationPaginationQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        Page page= (Page) categoryManagementMapper.classificationPaginationQuery(categoryPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据ID删除分类
     * @param id
     * @return
     */
    @Override
    public void deleteCategoryById(Integer id) {
        categoryManagementMapper.deleteCategoryById(id);
    }

    /**
     * 根据ID修改分类
     * @param category
     * @return
     */
    @Override
    public void changeClassificationById(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryManagementMapper.changeClassificationById(category);
    }

    /**
     * 启用禁用分类
     * @param status
     * @return
     */
    @Override
    public void enableAndDisableClassification(Integer status,Integer id) {
        categoryManagementMapper.enableAndDisableClassification(status,id);
    }


    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        long start = System.currentTimeMillis();
        List<Category> categoryList = (List<Category>) redisTemplate.opsForValue().get(RedisConstant.CATEGORY_LIST+type);
        long end = System.currentTimeMillis();
        if (categoryList==null || categoryList.size()==0){
            System.out.println("**********从数据库获取数据**********");
            categoryList=categoryManagementMapper.list(type);
            redisTemplate.opsForValue().set(RedisConstant.CATEGORY_LIST+type,categoryList);
        }else {
            System.out.println("**********从redis获取数据**********"+(end-start));
        }
        return categoryList;
    }

}
